import { tranListToTreeData } from '@/utils/tranListToTreeData.js'
import { get_company_department, del_company_department, get_user_simple } from '@/api/department'
const state = {
  depts: [],
  managers: []
}
const mutations = {
  set_company_department(state, data) {
    state.depts = data
  },
  set_user_simple(state, data) {
    state.managers = data
  }

}
const actions = {
  async get_company_department(store) {
    const res = await get_company_department()
    const root_list = await tranListToTreeData(res.depts, '')
    store.commit('set_company_department', root_list)
  },
  async del_company_department(store, id) {
    await del_company_department(id)
    store.dispatch('get_company_department')
  },
  async get_user_simple(store) {
    const res = await get_user_simple()
    const managers = res.map(item => item.username)
    store.commit('set_user_simple', managers)
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

